/************************************************************************

								Heroes IV
					  Copyright 2001, The 3DO Company

	------------------------------------------------------------------
  						 create_creature_button.cpp

	$Header: $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "create_creature_button.h"

#include "bitmap_group.h"
#include "bitmap_layer_cache_window.h"
#include "bitmap_layer_window.h"
#include "button.h"
#include "standard_fonts.h"
#include "text_window.h"

// ------------------------------------------------------------
// Set up a button with a hero or creature portraits and the
// standard set of rings
// ------------------------------------------------------------
void create_creature_button( t_button* button, t_bitmap_group* bitmaps,
							 t_bitmap_layer_ptr icon, std::string const& text, bool display_only,
							 std::list<t_bitmap_layer_cache_window_ptr> * image_list_ptr )
{
	t_window*			  holder;
	t_window*			  window;
	t_bitmap_layer_cache_window * image_window;
	t_bitmap_layer const* layer;
	t_screen_point        origin;
	t_screen_point        icon_offset;
	t_screen_rect         rect;

	// find "released" image
	layer = bitmaps->find( "ring_released" );
	origin = layer->get_rect().top_left();
	// compute offset for hero portrait
	icon_offset = bitmaps->find( "hero_icon" )->get_rect().top_left() - origin;
	rect = t_screen_rect(0,0,0,0);
	// create the "released" image
	holder = new t_window( rect, t_window::k_completely_transparent, button );
	image_window = new t_bitmap_layer_cache_window( icon, icon_offset, holder, false );
	window = new t_bitmap_layer_window( layer, -origin, holder );
	holder->set_size_from_children();
	button->set_released_image( holder );

	if ( image_list_ptr )
		image_list_ptr->push_back( image_window );

	if( !display_only )
	{
		// create the "pressed" image
		layer = bitmaps->find( "ring_pressed" );
		holder = new t_window( rect, t_window::k_completely_transparent, button );
		image_window = new t_bitmap_layer_cache_window( icon, icon_offset, holder, false );
		window = new t_bitmap_layer_window( layer, -origin, holder );
		holder->set_size_from_children();
		button->set_pressed_image( holder );

		if ( image_list_ptr )
			image_list_ptr->push_back( image_window );

		// create the "disabled" image
		layer = bitmaps->find( "ring_disabled" );
		holder = new t_window( rect, t_window::k_completely_transparent, button );
		image_window = new t_bitmap_layer_cache_window( icon, icon_offset, holder, false );
		window = new t_bitmap_layer_window( layer, -origin, holder );
		holder->set_size_from_children();
		button->set_disabled_image( holder );

		if ( image_list_ptr )
			image_list_ptr->push_back( image_window );
	}

	// create label
	if ( text.empty() )
		return;

	t_text_window* text_window;

	layer = bitmaps->find( "inset" );
	window = new t_bitmap_layer_window( layer, -origin, button );
	rect = bitmaps->find( "inset_text" )->get_rect() - origin;
	text_window = new t_text_window( get_font( rect.height() ), rect, button, text,
		                              t_pixel_24(0,0,0) );
	text_window->set_center_horizontal();
	text_window->set_drop_shadow( false );
}
